// packageD/directSeeding/anchorApply/anchorApply.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show1: false,
    show2: false,
    show3: false,
    show4: false,
    show5: false,
    addressName: "",
    districtVal: "",
    popAddAddress: false,
    strShow: false, //街道显示
    openDateLw: false,
    provinceData: [],
    cityData: [],
    districtData: [],
    pickerValue: [0, 0, 0],
    selectAddressData: [],
    streetShow: false, //街道pop
    form: {
      name: "",
      telephoneNumber: "",
      addressContent: "",
      provinceId: "",
      cityId: "",
      districtId: "",
      address: "",
      streetId: null,
      remarksContent: "",
      checked: "",
    },
    //协议内容
    agreementData: "",
    //主播申请状态
    status: 1,
    banner: "",
    condition: "",

    showBuyGoods: false, //需要购买指定商品才能申请主播
    showBuyGoodslist: {},
    currentWordNumber: 0,

    clickTag: false,
  },
  telephoneInp(e) {
    let val = e.detail.value;
    this.setData({
      "form.telephoneNumber": val,
    });
  },
  nameinp(e) {
    let val = e.detail.value;
    this.setData({
      "form.name": val,
    });
  },
  streetChoose() {
    if (app._isTextEmpty(this.data.addressName)) {
      wx.showToast({
        icon: "none",
        title: "请先选择所在地区",
        duration: 1500,
      });
    } else {
      this.setData({
        streetShow: true,
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //打开地址选择器
  openDateLwtbtn: function () {
    this.setData({
      openDateLw: true,
    });
  },
  onLoad: function (options) {},
  //关闭取消绑定手机页面
  show1CloseBtn() {
    this.setData({
      show1: false,
    });
  },
  verifyApplyStatus() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.room.verifyApplyStatus"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        validate_page: 1
      }, // validate_page 实名认证判断
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let status = res.data.status;
          that.setData({
            agreementData: res.data.agreement,
            banner: res.data.banner,
          });
          that.setData({
            status: status,
          });
          if (status == 2) {
            that.setData({
              show3: true,
            });
          }
          if (status == 3) {
            that.setData({
              show5: true,
              condition: res.data.condition,
            });
          }
          if (status == 4) {
            that.setData({
              show3: true,
            });
          }
          if (status == 5) {
            that.setData({
              show1: true,
            });
          }
          if (status == 6) {
            wx.showModal({
              title: "提示",
              content: "您需要购买指定的商品才能申请主播！",
              success(r) {
                if (r.confirm) {
                  that.setData({
                    showBuyGoodslist: res.data.goods,
                    showBuyGoods: true,
                  });
                } else if (r.cancel) {
                  console.log("用户点击取消");
                  wx.reLaunch({
                    url: '/packageG/member_v2/member_v2'
                  });
                }
              },
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  closedBuyGoodsPopup() {
    try {
      wx.navigateBack({
        delta: 1,
      });
    } catch (error) {
      wx.reLaunch({
        url: '/packageG/member_v2/member_v2'
      });
    }
  },
  gotoGoods(e) {
    this.setData({
      showBuyGoods: false,
    });
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + id,
    });
  },
  // 跳转到绑定手机号
  goEditMobile() {
    //跳转绑定手机页面
    wx.navigateTo({
      url: "/packageA/member/editmobile/editmobile?anchorApply=1",
    });
  },
  //跳转直播首页
  goLiveList() {
    wx.navigateTo({
      url: "/packageD/directSeeding/liveList/liveList",
    });
  },
  //跳转推广申请页
  goExtensionApply() {
    wx.navigateTo({
      url: "/packageA/member/extension/ExtensionApply/ExtensionApply",
    });
  },
  //跳转企业申请
  goStoreApply() {
    wx.navigateTo({
      url: "/packageA/member/storeApply/storeApply",
    });
  },
  //跳转供应商申请
  goSupplier() {
    wx.navigateTo({
      url: "/packageA/member/supplier/supplier",
    });
  },
  //跳转供应商申请
  gohotel() {
    wx.navigateTo({
      url: "/packageA/member/storeApply/storeApply?name=hotelApply",
    });
  },
  //地址选择器数据初始化
  _initAddressInfo: function () {
    var that = this;
    var _keyName = "provinceData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("地址信息准备就绪");
        that._getProvinceData();
      },
      fail: function (res) {
        console.log("省fail");
        app._getNetWork({
          url: app.getNetAddresss("member.member-address.address"),
          success: function (res) {
            var response = res.data;
            var province = response.data.province;
            var city = response.data.city;
            var district = response.data.district;
            wx.setStorage({
              key: "provinceData",
              data: province,
            });
            wx.setStorage({
              key: "cityData",
              data: city,
            });
            wx.setStorage({
              key: "districtData",
              data: district,
            });
            that._initAddressInfo();
          },
          fail: function (res) {},
          state: true,
        });
      },
    });
  },
  // 读取省key
  _getProvinceData: function () {
    var that = this;
    var _keyName = "provinceData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("省读取成功", res.data);
        that.setData({
          provinceData: res.data,
        });
        that.setData({
          "selectAddressData[0].name": res.data[that.data.pickerValue[0]].areaname,
          "selectAddressData[0].id": res.data[that.data.pickerValue[0]].id,
          "selectAddressData[0].index": that.data.pickerValue[0],
        });
        that._getCityData();
      },
    });
  },
  // 读取城市key，筛选城市data
  _getCityData: function () {
    var _data = this.data.selectAddressData[0];
    console.log("城市", "_data", _data);
    var that = this;
    var _keyName = "cityData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("城市读取成功", res);

        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        console.log("城市数据筛选", _json);
        that.setData({
          cityData: _json,
        });
        that.setData({
          "selectAddressData[1].name": _json[that.data.pickerValue[1]].areaname,
          "selectAddressData[1].id": _json[that.data.pickerValue[1]].id,
          "selectAddressData[1].index": that.data.pickerValue[1],
        });
        that._getDistrictData();
      },
    });
  },
  // 读取地区key，筛选地区data
  _getDistrictData: function () {
    var _data = this.data.selectAddressData[1];
    console.log("地区", "_data", _data);
    console.log(_data);
    var that = this;
    var _keyName = "districtData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("地区读取成功");
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        console.log("地区数据筛选", _json);
        that.setData({
          districtData: _json,
        });
        console.log("districtData", _json);
        that.setData({
          "selectAddressData[2].name": _json[that.data.pickerValue[2]].areaname,
          "selectAddressData[2].id": _json[that.data.pickerValue[2]].id,
          "selectAddressData[2].index": that.data.pickerValue[2],
        });
        console.log("over", that.data.selectAddressData);
      },
    });
  },
  //打开地址选择器

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      show1: false,
      show2: false,
      show3: false,
      show4: false,
      show5: false,
    });
    this.verifyApplyStatus();
    this._initAddressInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  // 收货地址滚动事件
  bindChange: function (e) {
    var val = e.detail.value;
    if (this.data.pickerValue[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
    }
    if (this.data.pickerValue[1] != val[1]) {
      val[2] = 0;
    }
    this.setData({
      "pickerValue[0]": val[0],
      "pickerValue[1]": val[1],
      "pickerValue[2]": val[2],
    });
    this._initAddressInfo();
  },
  _closeDateLw: function () {
    this.setData({
      openDateLw: false,
    });
  },
  _okAddress: function () {
    let seleData = this.data.selectAddressData;
    let addressName =
      seleData[0].name + " " + seleData[1].name + " " + seleData[2].name;
    let provinceId = seleData[0].id;
    let cityId = seleData[1].id;
    let districtId = seleData[2].id;
    this.setData({
      addressName: addressName,
      "form.provinceId": provinceId,
      "form.cityId": cityId,
      "form.districtId": districtId,
    });
    this._getStreet(seleData[2].id);
    this._closeDateLw();
  },
  _getStreet(param) {
    let that = this;
    let urlStr = app.getNetAddresss("member.memberAddress.street");
    urlStr += "&district_id=" + param;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1 && !app._isTextEmpty(res.data)) {
          if (!app._isTextEmpty(res.data) && res.data.length > 0) {
            that.setData({
              districtVal: res.data,
              strShow: true,
            });
          } else {
            that.setData({
              strShow: false,
            });
          }
        } else {
          that.setData({
            strShow: false,
          });
        }
      },
      fail: function (res) {
        that.setData({
          strShow: false,
        });
        console.log(res);
      },
    });
  },
  //选择街道
  streetConfirm(e) {
    let strname = e.target.dataset.strname;
    let strid = e.target.dataset.strid;
    this.setData({
      "form.street": strname,
      "form.streetId": strid,
      streetShow: false,
      opren: true,
    });
  },
  //关闭选择街道弹窗
  streetClose: function () {
    this.setData({
      streetShow: false,
      opren: true,
    });
  },
  addressInp(e) {
    let val = e.detail.value;
    this.setData({
      "form.addressContent": val,
    });
  },
  remarksInp(e) {
    let val = e.detail.value;
    let len = parseInt(val.length);
    this.setData({
      "form.remarksContent": val,
      currentWordNumber: len
    });
  },
  protocolBtn(e) {
    let val = e.detail;
    this.setData({
      "form.checked": val,
    });
  },
  openProtocol() {
    //申请协议跳转
    this.setData({
      show4: true,
    });
  },
  show4CloseBtn() {
    this.setData({
      show4: false,
    });
  },
  submit() {
    if (this.data.clickTag) {
      return;
    }
    let that = this;
    if (!this.data.form.name) {
      wx.showToast({
        title: "请输入您的姓名",
        icon: "none",
        duration: 1500,
      });
      return false;
    }
    if (!this.data.form.telephoneNumber) {
      wx.showToast({
        title: "手机号不能为空",
        icon: "none",
        duration: 1500,
      });
      return false;
    }
    if (
      this.data.form.telephoneNumber &&
      app._isMoblie(this.data.form.telephoneNumber)
    ) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        duration: 1500,
      });
      return false;
    }
    if (!this.data.addressName) {
      wx.showToast({
        title: "请选择收货地址",
        icon: "none",
        duration: 1500,
      });
      return false;
    }
    if (!this.data.form.streetId && this.data.form.streetId != 0 && this.data.strShow) {
      //零表示其他
      wx.showToast({
        title: "请选择街道",
        icon: "none",
        duration: 1500,
      });
      return false;
    }
    if (!this.data.form.addressContent) {
      wx.showToast({
        title: "请填写详细的地址",
        icon: "none",
        duration: 1500,
      });
      return false;
    }
    if (!this.data.form.checked) {
      wx.showToast({
        title: "请勾选用户协议",
        icon: "none",
        duration: 1500,
      });
      return false;
    }
    that.setData({
      clickTag: true,
    });
    let urlStr = app.getNetAddresss("plugin.room.frontend.room.apply");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        realname: this.data.form.name,
        mobile: this.data.form.telephoneNumber,
        provinceId: this.data.form.provinceId,
        cityId: this.data.form.cityId,
        districtId: this.data.form.districtId,
        streetId: this.data.form.streetId,
        address: this.data.form.addressContent,
        remark: this.data.form.remarksContent,
      },
      success: function (resdata) {
        var res = resdata.data;
        that.setData({
          clickTag: true,
        });
        if (res.result == 1) {
          if (
            res.data.jump_min_url != "" && res.data.jump_min_url != null &&
            res.data.jump_min_url != undefined
          ) {
            try {
              wx.navigateTo({
                url: res.data.jump_min_url,
              });
              return;
            } catch (err) {
              console.log(err);
            }
          }
          that.setData({
            show2: true,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
});